<template>
  <div class="convert-container">
    <!-- JSON卡片 -->
    <div class="convert-card">
      <!-- 内容 -->
      <div class="convert-inner">
        <DotGrid
          :dot-size="5"
          :gap="20"
          :base-color="cardBgColor"
          :active-color="cardFontColor"
          :proximity="150"
          :speed-trigger="100"
          :shock-radius="250"
          :shock-strength="5"
          :max-speed="5000"
          :resistance="750"
          :return-duration="1.5"
          class-name="custom-dot-grid"
        />
        <!-- 标题 -->
        <n-divider title-placement="left"> 一站式JSON工具 </n-divider>
        <!-- 按钮 -->
        <button class="btn-enter" @click="openJsonTools">
          <!-- 评分 -->
          <div class="original">json工具</div>
          <!-- 应用 -->
          <div class="letters">
            <span>前</span>
            <span>往</span>
            <span>使</span>
            <span>用</span>
          </div>
        </button>
      </div>
    </div>
    <!-- 卡片 -->
    <div class="convert-card">
      <!-- 内容 -->
      <div class="convert-inner">
        <DotGrid
          :dot-size="5"
          :gap="20"
          :base-color="cardBgColor"
          :active-color="cardFontColor"
          :proximity="150"
          :speed-trigger="100"
          :shock-radius="250"
          :shock-strength="5"
          :max-speed="5000"
          :resistance="750"
          :return-duration="1.5"
          class-name="custom-dot-grid"
        />
        <!-- 标题 -->
        <n-divider title-placement="left"> 图片工具合集 </n-divider>
        <!-- 按钮 -->
        <button class="btn-enter" @click="openImageTools">
          <!-- 评分 -->
          <div class="original">图片工具</div>
          <!-- 应用 -->
          <div class="letters">
            <span>前</span>
            <span>往</span>
            <span>使</span>
            <span>用</span>
          </div>
        </button>
      </div>
    </div>
    <!-- 卡片 -->
    <div class="convert-card">
      <!-- 内容 -->
      <div class="convert-inner"></div>
    </div>
    <!-- 卡片 -->
    <div class="convert-card">
      <!-- 内容 -->
      <div class="convert-inner"></div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useConfigStore } from "@renderer/store/config";

/* ******************************通用属性/方法****************************** */
const useConfig = useConfigStore();
const router = useRouter();

/* ******************************卡片属性/方法****************************** */
// 计算径向渐变
onMounted(() => {
  const containerEl = document.querySelector(".convert-container") as HTMLElement;
  const cardEls = document.querySelectorAll(".convert-card") as NodeListOf<HTMLElement>;

  // 鼠标移动事件
  containerEl.onmousemove = (e: MouseEvent) => {
    cardEls.forEach((card) => {
      if (!card) return; // 确保元素存在
      const rect = card.getBoundingClientRect(); // 获取容器的边界信息
      const x = e.clientX - rect.left - rect.width / 2; // 鼠标离视口的距离 - 容器左边缘的距离 - 容器宽度的一半
      const y = e.clientY - rect.top - rect.height / 2; // 同上

      card.style.setProperty("--x", `${x}px`); // 设置高亮区域的 X 坐标
      card.style.setProperty("--y", `${y}px`); // 设置高亮区域的 Y 坐标
    });
  };
});
// 卡片背景颜色
const cardBgColor = computed(() => (useConfig.theme === "light" ? "#f2f2f2" : "rgb(20,20,20)"));
// 卡片字体颜色
const cardFontColor = computed(() => (useConfig.theme === "light" ? "rgb(20, 20, 20)" : "#f2f2f2"));

// 打开JSON工具
const openJsonTools = () => {
  router.replace({ path: "/office/convert/jsonTools" });
};
// 打开图片工具
const openImageTools = () => {
  router.replace({ path: "/office/convert/imageTools" });
};
</script>

<style lang="scss" scoped>
.convert-container {
  display: grid;
  height: 100%;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 20px;

  // 第一层 卡片
  .convert-card {
    position: relative;
    overflow: hidden;
    border-radius: 8px;
    cursor: pointer;

    // 第二层 高亮区域
    &::before {
      content: "";
      position: absolute;
      inset: 0;
      // 径向渐变(最短边为半径，逐渐透明)
      background: radial-gradient(closest-side circle, v-bind(cardFontColor), transparent);
      // 移动到鼠标位置，给个初始值，避免初始时出现动画
      transform: translate(var(--x, -1000000px), var(--y, -1000000px));
    }

    // 第三层 内容
    .convert-inner {
      position: absolute;
      inset: 2px;
      padding: 2px;
      background-color: v-bind(cardBgColor);
      border-radius: inherit;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      .custom-dot-grid {
        position: absolute;
      }

      :deep(.n-divider__line) {
        height: 2px;
        background-color: v-bind(cardFontColor);
      }
    }
  }
}

.btn-enter,
.btn-enter *,
.btn-enter :after,
.btn-enter :before,
.btn-enter:after,
.btn-enter:before {
  border: 0 solid;
  box-sizing: border-box;
}

.btn-enter {
  -webkit-tap-highlight-color: transparent;
  mask-image: -webkit-radial-gradient(#000, #fff);
  appearance: button;
  background-color: v-bind(cardBgColor);
  background-image: none;
  color: v-bind(cardFontColor);
  cursor: pointer;
  line-height: 1.5;
  margin: 0;
  padding: 0;
}

.btn-enter:disabled {
  cursor: default;
}

.btn-enter:-moz-focusring {
  outline: auto;
}

.btn-enter svg {
  display: block;
  // vertical-align: middle;
}

.btn-enter [hidden] {
  display: none;
}

.btn-enter {
  border: 1px solid;
  border-radius: 999px;
  box-sizing: border-box;
  display: block;
  font-weight: 900;
  overflow: hidden;
  padding: 1.2rem 3rem;
  position: relative;
  text-transform: uppercase;
}

.btn-enter .original {
  background: v-bind(cardBgColor);
  color: v-bind(cardFontColor);
  display: grid;
  inset: 0;
  place-content: center;
  position: absolute;
  transition: transform 0.2s cubic-bezier(0.87, 0, 0.13, 1);
}

.btn-enter:hover .original {
  transform: translateY(100%);
}

.btn-enter .letters {
  display: inline-flex;
  // 文字间隔
  letter-spacing: 0.3em;
}

.btn-enter span {
  opacity: 0;
  transform: translateY(-15px);
  transition:
    transform 0.2s cubic-bezier(0.87, 0, 0.13, 1),
    opacity 0.2s;
}

.btn-enter span:nth-child(2n) {
  transform: translateY(15px);
}

.btn-enter:hover span {
  opacity: 1;
  transform: translateY(0);
}

.btn-enter:hover span:nth-child(2) {
  transition-delay: 0.1s;
}

.btn-enter:hover span:nth-child(3) {
  transition-delay: 0.2s;
}

.btn-enter:hover span:nth-child(4) {
  transition-delay: 0.3s;
}
</style>
